package com.education.module.index;

import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.education.domain.LoginUser;
import com.education.domain.Message;
import com.education.framework.base.BaseController;
import com.education.framework.session.SessionHelper;
import com.education.module.message.MessageServices;

@Controller
@RequestMapping("index")
public class IndexController extends BaseController{

	@Autowired
	private MessageServices services;
	
	@RequestMapping(value = "")
	public String list(Model model,ServletRequest request){
		List<Message> list = services.find("");
		model.addAttribute("list", list);
		return "/index/index";
	}
	@RequestMapping(value = "showList")
	public String showList(Model model, ServletRequest request){
		List<Message> list = services.find("");
		model.addAttribute("list", list);
		return "/index/showList";
	}
	
	@RequestMapping(value = "showContentPage/{id}")
	public String showContentPage(Model model,@PathVariable("id") Integer id,ServletRequest request){
		Message msg = services.findForObject(id);
		model.addAttribute("message", msg);
		return "/index/showContentPage";
	}
	
	@RequestMapping(value = "userLogin")
	public String userLogin(Model model,LoginUser loginUser,RedirectAttributes redirectAttributes){
		//验证
		boolean ret = services.checkLogin(loginUser);
		SessionHelper.getInstance().clearLoginUser();
		if(ret){
			//登录成功，保存SESSION
			SessionHelper.getInstance().putLoginUser(loginUser);
			System.out.println("登录成功！");
		}else{
			redirectAttributes.addFlashAttribute("MESSAGE", "登录失败,稍候请重试!");
			System.out.println("登录失败！");
			return "redirect:/index";
		}
		return "redirect:/teacherExamResult";
	}
	@RequestMapping(value = "userLogout")
	public String userLogout(Model model,RedirectAttributes redirectAttributes){
		SessionHelper.getInstance().clearLoginUser();
		return "redirect:/index";
	}
}
